<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width">
<meta name="theme-color" content="#222"><meta name="generator" content="Hexo 6.3.0">

  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/favicon.ico">
  <link rel="icon" type="image/png" sizes="16x16" href="/favicon-16x16.ico">
  <link rel="mask-icon" href="/images/logo.svg" color="#222">

<link rel="stylesheet" href="/css/main.css">



<link rel="stylesheet" href="https://fastly.jsdelivr.net/npm/@fortawesome/fontawesome-free@6.7.2/css/all.min.css" integrity="sha256-dABdfBfUoC8vJUBOwGVdm8L9qlMWaHTIfXt+7GnZCIo=" crossorigin="anonymous">
  <link rel="stylesheet" href="https://fastly.jsdelivr.net/npm/animate.css@3.1.1/animate.min.css" integrity="sha256-PR7ttpcvz8qrF57fur/yAx1qXMFJeJFiA6pSzWi0OIE=" crossorigin="anonymous">

<script class="next-config" data-name="main" type="application/json">{"hostname":"blog.csgrandeur.cn","root":"/","images":"/images","scheme":"Gemini","darkmode":false,"version":"8.22.0","exturl":false,"sidebar":{"position":"left","width_expanded":320,"width_dual_column":240,"display":"post","padding":18,"offset":12},"hljswrap":true,"copycode":{"enable":true,"style":"default"},"fold":{"enable":false,"height":500},"bookmark":{"enable":false,"color":"#222","save":"auto"},"mediumzoom":false,"lazyload":false,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"stickytabs":false,"motion":{"enable":true,"async":false,"duration":200,"transition":{"menu_item":"fadeInDown","post_block":"fadeIn","post_header":"fadeInDown","post_body":"fadeInDown","coll_header":"fadeInLeft","sidebar":"fadeInUp"}},"prism":false,"i18n":{"placeholder":"搜索...","empty":"没有找到任何搜索结果：${query}","hits_time":"找到 ${hits} 个搜索结果（用时 ${time} 毫秒）","hits":"找到 ${hits} 个搜索结果"},"path":"/search.xml","localsearch":{"enable":true,"top_n_per_article":1,"unescape":false,"preload":false,"trigger":"auto"}}</script><script src="/js/config.js"></script>

    <meta name="description" content="图结构与基本搜索 本章介绍图的基本概念、存储方式以及两种基本图搜索算法——深度优先搜索（DFS）和广度优先搜索（BFS），涵盖图的定义、分类、存储结构及其在代码中的实现，并提供DFS和BFS的典型模板代码。">
<meta property="og:type" content="article">
<meta property="og:title" content="11.图结构与基本搜索">
<meta property="og:url" content="http://blog.csgrandeur.cn/2025-03-12-11-%E5%9B%BE%E7%BB%93%E6%9E%84%E4%B8%8E%E5%9F%BA%E6%9C%AC%E6%90%9C%E7%B4%A2/index.html">
<meta property="og:site_name" content="CSGrandeur&#39;s Thinking">
<meta property="og:description" content="图结构与基本搜索 本章介绍图的基本概念、存储方式以及两种基本图搜索算法——深度优先搜索（DFS）和广度优先搜索（BFS），涵盖图的定义、分类、存储结构及其在代码中的实现，并提供DFS和BFS的典型模板代码。">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="http://blog.csgrandeur.cn/2025-03-12-11-%E5%9B%BE%E7%BB%93%E6%9E%84%E4%B8%8E%E5%9F%BA%E6%9C%AC%E6%90%9C%E7%B4%A2/graph_dire.svg">
<meta property="og:image" content="http://blog.csgrandeur.cn/2025-03-12-11-%E5%9B%BE%E7%BB%93%E6%9E%84%E4%B8%8E%E5%9F%BA%E6%9C%AC%E6%90%9C%E7%B4%A2/graph_sparse.svg">
<meta property="og:image" content="http://blog.csgrandeur.cn/2025-03-12-11-%E5%9B%BE%E7%BB%93%E6%9E%84%E4%B8%8E%E5%9F%BA%E6%9C%AC%E6%90%9C%E7%B4%A2/graph_save_graph.svg">
<meta property="og:image" content="http://blog.csgrandeur.cn/2025-03-12-11-%E5%9B%BE%E7%BB%93%E6%9E%84%E4%B8%8E%E5%9F%BA%E6%9C%AC%E6%90%9C%E7%B4%A2/graph_save_mat.svg">
<meta property="og:image" content="http://blog.csgrandeur.cn/2025-03-12-11-%E5%9B%BE%E7%BB%93%E6%9E%84%E4%B8%8E%E5%9F%BA%E6%9C%AC%E6%90%9C%E7%B4%A2/graph_board.png">
<meta property="article:published_time" content="2025-03-12T10:13:14.000Z">
<meta property="article:modified_time" content="2025-03-13T08:28:24.180Z">
<meta property="article:author" content="CSGrandeur">
<meta property="article:tag" content="ACM">
<meta property="article:tag" content="Algorithm">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="http://blog.csgrandeur.cn/2025-03-12-11-%E5%9B%BE%E7%BB%93%E6%9E%84%E4%B8%8E%E5%9F%BA%E6%9C%AC%E6%90%9C%E7%B4%A2/graph_dire.svg">


<link rel="canonical" href="http://blog.csgrandeur.cn/2025-03-12-11-%E5%9B%BE%E7%BB%93%E6%9E%84%E4%B8%8E%E5%9F%BA%E6%9C%AC%E6%90%9C%E7%B4%A2/">


<script class="next-config" data-name="page" type="application/json">{"sidebar":"","isHome":false,"isPost":true,"lang":"zh-CN","comments":true,"permalink":"http://blog.csgrandeur.cn/2025-03-12-11-%E5%9B%BE%E7%BB%93%E6%9E%84%E4%B8%8E%E5%9F%BA%E6%9C%AC%E6%90%9C%E7%B4%A2/","path":"2025-03-12-11-图结构与基本搜索/","title":"11.图结构与基本搜索"}</script>

<script class="next-config" data-name="calendar" type="application/json">""</script>
<title>11.图结构与基本搜索 | CSGrandeur's Thinking</title>
  

  <script src="/js/third-party/analytics/baidu-analytics.js"></script>
  <script async src="https://hm.baidu.com/hm.js?7958adf931092425a489778560129144"></script>







  <noscript>
    <link rel="stylesheet" href="/css/noscript.css">
  </noscript>
</head>

<body itemscope itemtype="http://schema.org/WebPage" class="use-motion">
  <div class="headband"></div>

  <main class="main">
    <div class="column">
      <header class="header" itemscope itemtype="http://schema.org/WPHeader"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏" role="button">
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/" class="brand" rel="start">
      <i class="logo-line"></i>
      <p class="site-title">CSGrandeur's Thinking</p>
      <i class="logo-line"></i>
    </a>
      <p class="site-subtitle" itemprop="description">Cogito Ergo Sum</p>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger" aria-label="搜索" role="button">
        <i class="fa fa-search fa-fw fa-lg"></i>
    </div>
  </div>
</div>



<nav class="site-nav">
  <ul class="main-menu menu"><li class="menu-item menu-item-home"><a href="/" rel="section"><i class="fa fa-home fa-fw"></i>首页</a></li><li class="menu-item menu-item-categories"><a href="/categories/" rel="section"><i class="fa fa-th fa-fw"></i>分类</a></li><li class="menu-item menu-item-archives"><a href="/archives/" rel="section"><i class="fa fa-archive fa-fw"></i>归档</a></li>
      <li class="menu-item menu-item-search">
        <a role="button" class="popup-trigger"><i class="fa fa-search fa-fw"></i>搜索
        </a>
      </li>
  </ul>
</nav>



  <div class="search-pop-overlay">
    <div class="popup search-popup">
      <div class="search-header">
        <span class="search-icon">
          <i class="fa fa-search"></i>
        </span>
        <div class="search-input-container">
          <input autocomplete="off" autocapitalize="off" maxlength="80"
                placeholder="搜索..." spellcheck="false"
                type="search" class="search-input">
        </div>
        <span class="popup-btn-close" role="button">
          <i class="fa fa-times-circle"></i>
        </span>
      </div>
      <div class="search-result-container">
        <div class="search-result-icon">
          <i class="fa fa-spinner fa-pulse fa-5x"></i>
        </div>
      </div>
    </div>
  </div>

</header>
        
  
  <aside class="sidebar">

    <div class="sidebar-inner sidebar-nav-active sidebar-toc-active">
      <ul class="sidebar-nav">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <div class="sidebar-panel-container">
        <!--noindex-->
        <div class="post-toc-wrap sidebar-panel">
            <div class="post-toc animated"><ol class="nav"><li class="nav-item nav-level-1"><a class="nav-link" href="#%E5%9B%BE%E7%BB%93%E6%9E%84%E4%B8%8E%E5%9F%BA%E6%9C%AC%E6%90%9C%E7%B4%A2"><span class="nav-number">1.</span> <span class="nav-text">图结构与基本搜索</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#%E5%9B%BE"><span class="nav-number">1.1.</span> <span class="nav-text">图</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E5%9B%BE%E7%9A%84%E5%AD%98%E5%82%A8%E6%96%B9%E5%BC%8F"><span class="nav-number">1.2.</span> <span class="nav-text">图的存储方式</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#%E9%A1%BA%E5%BA%8F%E5%AD%98%E5%82%A8%E9%82%BB%E6%8E%A5%E7%9F%A9%E9%98%B5"><span class="nav-number">1.2.1.</span> <span class="nav-text">顺序存储（邻接矩阵）</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E9%93%BE%E5%BC%8F%E5%AD%98%E5%82%A8%E7%AB%9E%E8%B5%9B%E9%80%9A%E5%B8%B8%E7%94%A8%E9%93%BE%E5%BC%8F%E5%89%8D%E5%90%91%E6%98%9F"><span class="nav-number">1.2.2.</span> <span class="nav-text">链式存储，竞赛通常用链式前向星</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E6%A3%8B%E7%9B%98%E7%B1%BB%E5%9B%BE%E9%97%AE%E9%A2%98"><span class="nav-number">1.2.3.</span> <span class="nav-text">棋盘类图问题</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E5%9F%BA%E6%9C%AC%E7%9A%84%E6%90%9C%E7%B4%A2"><span class="nav-number">1.3.</span> <span class="nav-text">基本的搜索</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#%E6%B7%B1%E5%BA%A6%E4%BC%98%E5%85%88%E6%90%9C%E7%B4%A2deep-first-search-dfs"><span class="nav-number">1.3.1.</span> <span class="nav-text">深度优先搜索（Deep First
Search, DFS）</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E5%B9%BF%E5%BA%A6%E4%BC%98%E5%85%88%E6%90%9C%E7%B4%A2breath-first-search-bfs"><span class="nav-number">1.3.2.</span> <span class="nav-text">广度优先搜索（Breath First
Search, BFS）</span></a></li></ol></li></ol></li></ol></div>
        </div>
        <!--/noindex-->

        <div class="site-overview-wrap sidebar-panel">
          <div class="site-author animated" itemprop="author" itemscope itemtype="http://schema.org/Person">
  <p class="site-author-name" itemprop="name">CSGrandeur</p>
  <div class="site-description" itemprop="description"></div>
</div>
<div class="site-state-wrap animated">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
        <a href="/archives/">
          <span class="site-state-item-count">72</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
          <a href="/categories/">
        <span class="site-state-item-count">6</span>
        <span class="site-state-item-name">分类</span></a>
      </div>
      <div class="site-state-item site-state-tags">
          <a href="/tags/">
        <span class="site-state-item-count">22</span>
        <span class="site-state-item-name">标签</span></a>
      </div>
  </nav>
</div>

        </div>
      </div>
    </div>

    
  </aside>


    </div>

    <div class="main-inner post posts-expand">


  


<div class="post-block">
  
  

  <article itemscope itemtype="http://schema.org/Article" class="post-content" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="http://blog.csgrandeur.cn/2025-03-12-11-%E5%9B%BE%E7%BB%93%E6%9E%84%E4%B8%8E%E5%9F%BA%E6%9C%AC%E6%90%9C%E7%B4%A2/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.gif">
      <meta itemprop="name" content="CSGrandeur">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="CSGrandeur's Thinking">
      <meta itemprop="description" content="">
    </span>

    <span hidden itemprop="post" itemscope itemtype="http://schema.org/CreativeWork">
      <meta itemprop="name" content="11.图结构与基本搜索 | CSGrandeur's Thinking">
      <meta itemprop="description" content="">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          11.图结构与基本搜索
        </h1>

        <div class="post-meta-container">
          <div class="post-meta">
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar"></i>
      </span>
      <span class="post-meta-item-text">发表于</span>

      <time title="创建时间：2025-03-12 18:13:14" itemprop="dateCreated datePublished" datetime="2025-03-12T18:13:14+08:00">2025-03-12</time>
    </span>
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar-check"></i>
      </span>
      <span class="post-meta-item-text">更新于</span>
      <time title="修改时间：2025-03-13 16:28:24" itemprop="dateModified" datetime="2025-03-13T16:28:24+08:00">2025-03-13</time>
    </span>
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-folder"></i>
      </span>
      <span class="post-meta-item-text">分类于</span>
        <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
          <a href="/categories/ACM/" itemprop="url" rel="index"><span itemprop="name">ACM</span></a>
        </span>
          ，
        <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
          <a href="/categories/ACM/ACMCOURSE/" itemprop="url" rel="index"><span itemprop="name">ACMCOURSE</span></a>
        </span>
    </span>

  
</div>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody"><h1 id="图结构与基本搜索">图结构与基本搜索</h1>
<p>本章介绍图的基本概念、存储方式以及两种基本图搜索算法——深度优先搜索（DFS）和广度优先搜索（BFS），涵盖图的定义、分类、存储结构及其在代码中的实现，并提供DFS和BFS的典型模板代码。</p>
<span id="more"></span>
<h2 id="图">图</h2>
<p>由顶点（节点）和边组成的一种非线性数据结构，用于表示对象之间的关系，顶点代表对象，边代表对象之间的连接或关系。</p>
<p>有向图与无向图</p>
<img src="/2025-03-12-11-%E5%9B%BE%E7%BB%93%E6%9E%84%E4%B8%8E%E5%9F%BA%E6%9C%AC%E6%90%9C%E7%B4%A2/graph_dire.svg" class="" title="有向图与无向图">
<p>图的稀疏与稠密</p>
<img src="/2025-03-12-11-%E5%9B%BE%E7%BB%93%E6%9E%84%E4%B8%8E%E5%9F%BA%E6%9C%AC%E6%90%9C%E7%B4%A2/graph_sparse.svg" class="" title="图的稀疏与稠密">
<p>概念大全：</p>
<ul>
<li>图：由顶点（V）与连接顶点的边（E）构成</li>
<li>无向图：每条边没有方向的定义</li>
<li>有向图：每条边有方向，例如表达路的话，一条边就表达一条单向车道</li>
<li>完全图：任意两个顶点之间都有边（对于有向图则两个方向的边都有）</li>
<li>稀疏图：边较少的图</li>
<li>稠密图：边较多的图</li>
<li>顶点的度：与该点关联的边的个数，如果是有向图则分“出度”和“入度”</li>
<li>路径：连续的边构成的顶点序列</li>
<li>路径长度：路径的边长度之和（如果没长度可以是边数量之和）</li>
<li>回路（环）：第一个顶点和最后一个顶点相同的路径</li>
<li>简单路径：除起点和终点外，其余顶点皆不相同</li>
<li>简单回路：起点终点相同，其他顶点皆不相同</li>
<li>连通图：图中任意两个顶点之间都能找到一个路径</li>
<li>子图：如果图B的顶点和边都是图A的顶点和边的子集，则B是A的子图</li>
<li>连通分量（强连通分量）：图B是图A的子图且图B是连通图（即A的连通子图），A中任意不在B中的顶点加入B后，B不再连通，这样的B是A的连通分量</li>
<li>极小连通子图：图B是图A的连通子图，如果B任意删除一条边后都不再连通，则B是A的极小连通子图</li>
<li>生成树：包含图 G 的所有顶点的极小连通子图，是图G的生成树</li>
<li>生成森林：非连通图的各个连通分量的生成树的集合</li>
<li>最小生成树：边有边权（或长度或其他量），边权之和最小的生成树</li>
</ul>
<h2 id="图的存储方式">图的存储方式</h2>
<p>在代码中存这个图：</p>
<img src="/2025-03-12-11-%E5%9B%BE%E7%BB%93%E6%9E%84%E4%B8%8E%E5%9F%BA%E6%9C%AC%E6%90%9C%E7%B4%A2/graph_save_graph.svg" class="" title="存储一个图">
<h3 id="顺序存储邻接矩阵">顺序存储（邻接矩阵）</h3>
<img src="/2025-03-12-11-%E5%9B%BE%E7%BB%93%E6%9E%84%E4%B8%8E%E5%9F%BA%E6%9C%AC%E6%90%9C%E7%B4%A2/graph_save_mat.svg" class="" title="邻接矩阵存法">
<p>用一个二维数组存储点与点的关系</p>
<ul>
<li><code>n</code> 个顶点， <code>e</code> 条边</li>
<li>对于无边权的图，<code>g[i][j] == 1</code>表示顶点 <code>i</code>
与顶点 <code>j</code> 有边，否则无边</li>
<li>对于有边权的图，可以额外用 <code>w[i][j]</code> 表示边的权值</li>
<li>无向图则 <code>g[i][j] == g[j][i]</code></li>
<li>适合<strong style="color:red;">稠密图</strong></li>
</ul>
<h3
id="链式存储竞赛通常用链式前向星">链式存储，竞赛通常用链式前向星</h3>
<ul>
<li><code>n</code> 个顶点，<code>e</code>条边</li>
<li>每一个顶点， 都用一个链表表示与其相连的边</li>
<li>适合<strong style="color:red;">稀疏图</strong></li>
<li>对于无向图，则两个方向各建一条有向边</li>
</ul>
<blockquote>
<p>知识点：在面对一道图问题时，一定要先分析图的稠密程度，再决定用邻接矩阵还是链式前向星。
Tip：大多问题都可以用链式前向星，在一些显著稠密、卡常数的问题中，考虑用邻接矩阵，邻接矩阵代码也相对好写一些。</p>
</blockquote>
<p>链式前向星模板参考</p>
<figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br></pre></td><td class="code"><pre><span class="line"><span class="type">const</span> <span class="type">int</span> maxN = <span class="number">1100</span>;</span><br><span class="line"><span class="type">const</span> <span class="type">int</span> maxM = <span class="number">110000</span>;</span><br><span class="line"><span class="type">int</span> first[maxN];    <span class="comment">// 每个顶点发出的边的边链表头结点，该数组可初始化为 -1 表示每个顶点都还没有边</span></span><br><span class="line"><span class="type">int</span> nex[maxM];      <span class="comment">// 同个顶点发出的边的边结点 next 域</span></span><br><span class="line"><span class="type">int</span> u[maxM];        <span class="comment">// 边的发出顶点</span></span><br><span class="line"><span class="type">int</span> v[maxM];        <span class="comment">// 边的收入顶点</span></span><br><span class="line"><span class="type">int</span> w[maxM];        <span class="comment">// 边的权值</span></span><br><span class="line"><span class="type">int</span> tp;             <span class="comment">// 全局“内存分配”“指针”，就是模拟分配内存时，tp从0开始逐个增加</span></span><br><span class="line"></span><br><span class="line"><span class="comment">// 比如 first[1]，表示顶点 V1 发出的第一条边的“指针”，这里就是数组编号</span></span><br><span class="line"><span class="comment">// nex[first[1]] 表示顶点 V1 发出的边的链表的第二个结点编号</span></span><br><span class="line"><span class="comment">// nex[nex[first[1]]] 表示顶点 V1 发出的边的链表的第三个结点编号 ...</span></span><br><span class="line"><span class="comment">// u[first[1]] 顶点 V1</span></span><br><span class="line"><span class="comment">// v[first[1]] 顶点 V1 发出的第一条边的另一端的顶点编号，比如 v[first[1]] == 3 就表示 V1 连着 V3</span></span><br><span class="line"><span class="comment">// w[first[1]] 顶点 V1 发出的第一条边的权值</span></span><br><span class="line"></span><br><span class="line"><span class="function"><span class="type">void</span> <span class="title">AddEdge</span><span class="params">(<span class="type">int</span> s, <span class="type">int</span> e, <span class="type">int</span> weight)</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    <span class="comment">// 建图：表示顶点 s 向顶点 e 发出了一条权重为 weight 的有向边</span></span><br><span class="line">    <span class="comment">// 程序开始时 tp 初始为 0</span></span><br><span class="line">    nex[tp] = first[s]; <span class="comment">// 类似链表头插法</span></span><br><span class="line">    first[s] = tp;</span><br><span class="line">    u[tp] = s;</span><br><span class="line">    v[tp] = e;</span><br><span class="line">    w[tp] = weight;</span><br><span class="line">    tp ++;</span><br><span class="line">&#125;</span><br><span class="line"><span class="function"><span class="type">void</span> <span class="title">DbEdge</span><span class="params">(<span class="type">int</span> s, <span class="type">int</span> e, <span class="type">int</span> weight)</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    <span class="comment">// 如果表示无向图，那简单，两个方向都建一条有向边就好</span></span><br><span class="line">    <span class="built_in">AddEdge</span>(s, e, weight);</span><br><span class="line">    <span class="built_in">AddEdge</span>(e, s, weight);</span><br><span class="line">    <span class="comment">// 结合 tp 的属性，你会发现，可以很容易找到两个顶点之间成对的双向边</span></span><br><span class="line">    <span class="comment">// 比如 i 是 s 发向 e 的边的编号，那么 i^1 （异或操作） 就是 e 发向 s 的边</span></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="type">bool</span> vis[maxN] = &#123;<span class="number">0</span>&#125;;</span><br><span class="line"><span class="function"><span class="type">void</span> <span class="title">DFS</span><span class="params">(<span class="type">int</span> now)</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    <span class="comment">// 以深度优先搜索为例遍历全图，感受前向星的使用</span></span><br><span class="line">    <span class="keyword">for</span>(<span class="type">int</span> i = first[now]; i != <span class="number">-1</span>; i = nex[i])</span><br><span class="line">    &#123;</span><br><span class="line">        <span class="comment">// 类似链表访问过程，i = first[now] 从头结点获得第一条边的指针</span></span><br><span class="line">        <span class="comment">// i = nex[i] 即链表指针域往后遍历</span></span><br><span class="line">        <span class="comment">// i != -1 即判断是否到链表末尾</span></span><br><span class="line">        <span class="comment">// u[i]、v[i]、w[i] 都是链表结点的数据域，当然你可以把 nex、u、v、w 封装在 struct 里</span></span><br><span class="line">        <span class="keyword">if</span>(!vis[v[i]])</span><br><span class="line">        &#123;</span><br><span class="line">            vis[v[i]] = <span class="literal">true</span>;</span><br><span class="line">            <span class="built_in">DFS</span>(v[i]);</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h3 id="棋盘类图问题">棋盘类图问题</h3>
<p>迷宫、下棋等问题，节点即格点或格子，与其相关的是上下左右四个方向，或者加上斜向共8个方向，用邻接矩阵或链式前向星建图就太麻烦了，可以直接用<code>xy</code>坐标来表达节点，增减<code>xy</code>来找相邻节点。</p>
<img src="/2025-03-12-11-%E5%9B%BE%E7%BB%93%E6%9E%84%E4%B8%8E%E5%9F%BA%E6%9C%AC%E6%90%9C%E7%B4%A2/graph_board.png" class="" title="棋盘类问题">
<h2 id="基本的搜索">基本的搜索</h2>
<h3 id="深度优先搜索deep-first-search-dfs">深度优先搜索（Deep First
Search, DFS）</h3>
<p>深度优先搜索，用栈（递归的形式）</p>
<p>一条路走到底，往回一步，再一条路走到底，往回一步...一般用于求全部解、求一些很“深”的解</p>
<ol type="1">
<li>访问起始点v;</li>
<li>若v的第1个邻接点没访问过，深度遍历此邻接点；</li>
<li>若当前邻接点已访问过，再找v的第2个邻接点重新遍历；</li>
<li>如果所有邻接点都已访问，则返回上一个访问的顶点。</li>
</ol>
<p>在网格上DFS典型模板</p>
<figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line"><span class="type">const</span> <span class="type">int</span> maxn = <span class="number">110</span>;</span><br><span class="line"><span class="type">int</span> dx[<span class="number">4</span>] = &#123;<span class="number">-1</span>, <span class="number">1</span>, <span class="number">0</span>, <span class="number">0</span>&#125;;</span><br><span class="line"><span class="type">int</span> dy[<span class="number">4</span>] = &#123;<span class="number">0</span>, <span class="number">0</span>, <span class="number">-1</span>, <span class="number">1</span>&#125;;</span><br><span class="line"><span class="type">int</span> graph[maxn][maxn];</span><br><span class="line"><span class="type">bool</span> visited[maxn][maxn];</span><br><span class="line"><span class="type">int</span> endX, endY;</span><br><span class="line"><span class="function"><span class="type">int</span> <span class="title">DFS</span><span class="params">(<span class="type">int</span> x, <span class="type">int</span> y)</span> </span>&#123;</span><br><span class="line">    <span class="keyword">if</span> (x == endX &amp;&amp; y == endY) <span class="keyword">return</span> <span class="literal">true</span>;</span><br><span class="line">    <span class="keyword">for</span> (<span class="type">int</span> i = <span class="number">0</span>; i &lt; <span class="number">4</span>; i++) &#123;</span><br><span class="line">        <span class="type">int</span> nextX = x + dx[i], nextY = y + dy[i];</span><br><span class="line">        <span class="comment">// 把 graph[0][各列] 和 graph[各行][0] 设为不能走的哨兵，让实际数据从 1 开始，就不用额外判断坐标范围了</span></span><br><span class="line">        <span class="keyword">if</span> (graph[nextX][nextY] &amp;&amp; !visited[nextX][nextY]) &#123;</span><br><span class="line">            <span class="comment">// 这里 graph[i][j] 为 1 表示能走，为 0 表示不能走</span></span><br><span class="line">            visited[nextX][nextY] = <span class="literal">true</span>;</span><br><span class="line">            <span class="keyword">if</span> (<span class="built_in">DFS</span>(nextX, nextY)) <span class="keyword">return</span> <span class="literal">true</span>;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">return</span> <span class="literal">false</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>在图上DFS典型模板</p>
<figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="type">const</span> <span class="type">int</span> maxn;</span><br><span class="line"><span class="type">int</span> g[maxn][maxn];  <span class="comment">// 邻接矩阵</span></span><br><span class="line"><span class="type">bool</span> vis[maxn];</span><br><span class="line"><span class="function"><span class="type">void</span> <span class="title">DFS</span><span class="params">(<span class="type">int</span> now, <span class="type">int</span> n)</span> </span>&#123;</span><br><span class="line">    <span class="comment">// now: 当前顶点编号</span></span><br><span class="line">    <span class="comment">// n: 顶点个数</span></span><br><span class="line">    <span class="keyword">if</span> (vis[now]) <span class="keyword">return</span>;</span><br><span class="line">    vis[now] = <span class="literal">true</span>;</span><br><span class="line">    <span class="keyword">for</span> (<span class="type">int</span> i = <span class="number">0</span>; i &lt; n; i++) &#123;</span><br><span class="line">        <span class="keyword">if</span> (g[now][i])  <span class="comment">// now 与 i 之间有边</span></span><br><span class="line">            <span class="built_in">DFS</span>(i, n);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h3 id="广度优先搜索breath-first-search-bfs">广度优先搜索（Breath First
Search, BFS）</h3>
<p>广度优先搜索是一种分层的搜索过程，每向前走一步可能访问一批顶点，不像深度优先搜索那样有回退的情况，因此，广度优先搜索不是一个递归的过程，其算法也不是递归的，就近访问，一圈圈外扩一般用于求最短的路、最近的解。</p>
<ol type="1">
<li>在访问了起始点v之后，将 v 的尚未访问的邻接点放入访问队列；</li>
<li>在队列中出队尚未访问的顶点，进行访问</li>
<li>直到所有顶点都被访问</li>
</ol>
<p>在网格上BFS典型模板</p>
<figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">struct</span> <span class="title class_">Node</span> &#123;</span><br><span class="line">    <span class="type">int</span> x, y;</span><br><span class="line">    <span class="built_in">Node</span>() &#123; x = y = <span class="number">0</span>; &#125;</span><br><span class="line">    <span class="built_in">Node</span>(<span class="type">int</span> x_, <span class="type">int</span> y_) &#123;</span><br><span class="line">        x = x_;</span><br><span class="line">        y = y_;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;;</span><br><span class="line"><span class="function"><span class="type">int</span> <span class="title">BFS</span><span class="params">(<span class="type">int</span> startX, <span class="type">int</span> startY)</span> </span>&#123;</span><br><span class="line">    std::queue&lt;Node&gt; q;</span><br><span class="line">    q.<span class="built_in">push</span>(<span class="built_in">Node</span>(startX, startY));</span><br><span class="line">    visited[startX][startY] = <span class="literal">true</span>;</span><br><span class="line">    <span class="keyword">while</span> (!q.<span class="built_in">empty</span>()) &#123;</span><br><span class="line">        Node now = q.<span class="built_in">front</span>();</span><br><span class="line">        q.<span class="built_in">pop</span>();</span><br><span class="line">        <span class="keyword">if</span> (now.x == endX &amp;&amp; now.y == endY) <span class="keyword">return</span> <span class="literal">true</span>;</span><br><span class="line">        <span class="keyword">for</span> (<span class="type">int</span> i = <span class="number">0</span>; i &lt; <span class="number">4</span>; i++) &#123;</span><br><span class="line">            <span class="type">int</span> nextX = now.x + dx[i], nextY = now.y + dy[i];</span><br><span class="line">            <span class="keyword">if</span> (graph[nextX][nextY] &amp;&amp; !visited[nextX][nextY]) &#123;</span><br><span class="line">                visited[nextX][nextY] = <span class="literal">true</span>;</span><br><span class="line">                q.<span class="built_in">push</span>(<span class="built_in">Node</span>(nextX, nextY));</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">return</span> <span class="literal">false</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>在图上BFS典型模板</p>
<figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line"><span class="type">const</span> <span class="type">int</span> maxn;</span><br><span class="line"><span class="type">int</span> g[maxn][maxn];  <span class="comment">// 邻接矩阵</span></span><br><span class="line"><span class="type">bool</span> vis[maxn];</span><br><span class="line"><span class="function"><span class="type">void</span> <span class="title">BFS</span><span class="params">(<span class="type">int</span> start, <span class="type">int</span> n)</span> </span>&#123;</span><br><span class="line">    queue&lt;<span class="type">int</span>&gt; q;</span><br><span class="line">    q.<span class="built_in">push</span>(start);</span><br><span class="line">    vis[start] = <span class="literal">true</span>;</span><br><span class="line">    <span class="keyword">while</span> (!q.<span class="built_in">empty</span>()) &#123;</span><br><span class="line">        <span class="type">int</span> now = q.<span class="built_in">front</span>();</span><br><span class="line">        q.<span class="built_in">pop</span>();</span><br><span class="line">        <span class="keyword">for</span> (<span class="type">int</span> i = <span class="number">0</span>; i &lt; n; i++) &#123;</span><br><span class="line">            <span class="keyword">if</span> (g[now][i] &amp;&amp; !vis[i]) &#123;</span><br><span class="line">                vis[i] = <span class="literal">true</span>;</span><br><span class="line">                q.<span class="built_in">push</span>(i);</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

    </div>

    
    
    

    <footer class="post-footer">
          <div class="post-tags">
              <a href="/tags/ACM/" rel="tag"># ACM</a>
              <a href="/tags/Algorithm/" rel="tag"># Algorithm</a>
          </div>

        

          <div class="post-nav">
            <div class="post-nav-item">
                <a href="/2025-03-12-10-%E5%93%88%E5%B8%8C%E4%B8%8E%E5%AD%97%E5%85%B8%E6%A0%91/" rel="prev" title="10.哈希与字典树">
                  <i class="fa fa-angle-left"></i> 10.哈希与字典树
                </a>
            </div>
            <div class="post-nav-item">
                <a href="/2025-03-18-12-%E6%A8%A1%E6%8B%9F%E4%B8%8E%E9%AB%98%E7%B2%BE%E5%BA%A6/" rel="next" title="12.模拟与高精度">
                  12.模拟与高精度 <i class="fa fa-angle-right"></i>
                </a>
            </div>
          </div>
    </footer>
  </article>
</div>






    <div class="comments utterances-container"></div>
</div>
  </main>

  <footer class="footer">
    <div class="footer-inner">

  <div class="copyright">
    &copy; 
    <span itemprop="copyrightYear">2025</span>
    <span class="with-love">
      <i class="fa fa-heart"></i>
    </span>
    <span class="author" itemprop="copyrightHolder">CSGrandeur</span>
  </div>
  <div class="powered-by">由 <a href="https://hexo.io/" rel="noopener" target="_blank">Hexo</a> & <a href="https://theme-next.js.org/" rel="noopener" target="_blank">NexT.Gemini</a> 强力驱动
  </div>

    </div>
  </footer>

  
  <div class="toggle sidebar-toggle" role="button">
    <span class="toggle-line"></span>
    <span class="toggle-line"></span>
    <span class="toggle-line"></span>
  </div>
  <div class="sidebar-dimmer"></div>
  <div class="back-to-top" role="button" aria-label="返回顶部">
    <i class="fa fa-arrow-up fa-lg"></i>
    <span>0%</span>
  </div>

<noscript>
  <div class="noscript-warning">Theme NexT works best with JavaScript enabled</div>
</noscript>


  
  <script src="https://fastly.jsdelivr.net/npm/animejs@3.2.1/lib/anime.min.js" integrity="sha256-XL2inqUJaslATFnHdJOi9GfQ60on8Wx1C2H8DYiN1xY=" crossorigin="anonymous"></script>
  <script src="https://fastly.jsdelivr.net/npm/@next-theme/pjax@0.6.0/pjax.min.js" integrity="sha256-vxLn1tSKWD4dqbMRyv940UYw4sXgMtYcK6reefzZrao=" crossorigin="anonymous"></script>
<script src="/js/comments.js"></script><script src="/js/utils.js"></script><script src="/js/motion.js"></script><script src="/js/sidebar.js"></script><script src="/js/next-boot.js"></script><script src="/js/pjax.js"></script>

  <script src="https://fastly.jsdelivr.net/npm/hexo-generator-searchdb@1.4.1/dist/search.js" integrity="sha256-1kfA5uHPf65M5cphT2dvymhkuyHPQp5A53EGZOnOLmc=" crossorigin="anonymous"></script>
<script src="/js/third-party/search/local-search.js"></script>







  




  

  <script class="next-config" data-name="enableMath" type="application/json">true</script><script class="next-config" data-name="mathjax" type="application/json">{"enable":true,"tags":"none","js":{"url":"https://fastly.jsdelivr.net/npm/mathjax@3.2.2/es5/tex-mml-chtml.js","integrity":"sha256-MASABpB4tYktI2Oitl4t+78w/lyA+D7b/s9GEP0JOGI="}}</script>
<script src="/js/third-party/math/mathjax.js"></script>


<script class="next-config" data-name="utterances" type="application/json">{"enable":true,"repo":"CSGrandeur/csgrandeur.github.io","issue_term":"pathname","theme":"github-light"}</script>
<script src="/js/third-party/comments/utterances.js"></script>

</body>
</html>
